package com.example.springsecurity.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springsecurity.common.annotation.SystemLogInterface;
import com.example.springsecurity.common.enums.BusinessTypeEnum;
import com.example.springsecurity.common.resp.ResponseResult;
import com.example.springsecurity.entity.SystemUser;
import com.example.springsecurity.service.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

import static com.example.springsecurity.common.resp.ResponseResult.success;

/**
 * 用户信息表(SystemUser)表控制层
 *
 * @author aman
 * @since 2025-01-15 16:58:33
 */
@Tag(name = "系统用户管理")
@RestController
@RequestMapping("/system/user")
public class SystemUserController {
    /**
     * 服务对象
     */
    @Resource
    private SystemUserService systemUserService;

    /**
     * 分页查询用户列表
     *
     * @param page       分页对象
     * @param systemUser 查询实体
     * @return 所有数据
     */
    @Operation(summary = "分页查询用户列表")
    @PostMapping("/queryUserList")
    @SystemLogInterface(title = "分页查询用户列表", businessType = BusinessTypeEnum.QUERY)
    public ResponseResult<IPage<SystemUser>> queryUserList(Page<SystemUser> page, SystemUser systemUser) {
        return success(this.systemUserService.page(page, new QueryWrapper<>(systemUser)));
    }

    /**
     * 添加用户信息
     *
     * @param systemUser 实体对象
     * @return 新增结果
     */
    @Operation(summary = "添加用户信息")
    @PostMapping("/insertUser")
    @SystemLogInterface(title = "添加用户信息", businessType = BusinessTypeEnum.INSERT)
    public ResponseResult<Boolean> insertUser(@RequestBody SystemUser systemUser) {
        return success(this.systemUserService.save(systemUser));
    }

    /**
     * 更新用户信息
     *
     * @param systemUser 实体对象
     * @return 修改结果
     */
    @Operation(summary = "更新用户信息")
    @PostMapping("/updateUser")
    @SystemLogInterface(title = "更新用户信息", businessType = BusinessTypeEnum.UPDATE)
    public ResponseResult<Boolean> updateUser(@RequestBody SystemUser systemUser) {
        return success(this.systemUserService.updateById(systemUser));
    }

    /**
     * 删除用户信息
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @Operation(summary = "删除用户信息")
    @PostMapping("/deleteUser")
    @SystemLogInterface(title = "删除用户信息", businessType = BusinessTypeEnum.DELETE)
    public ResponseResult<Boolean> deleteUser(@RequestBody List<String> idList) {
        return success(this.systemUserService.removeByIds(idList));
    }
}
